package com.bwhx.eruler.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.bwhx.eruler.bean.InputRuler;
import com.bwhx.eruler.bean.Page;
import com.bwhx.eruler.bean.Ruler;
import com.bwhx.eruler.service.InputRulerService;
import com.bwhx.eruler.service.RulerService;
/**
 * @author Cuilitang
 *
 */

@Controller
public class InputRulerController extends BaseController {
	
	@Autowired
	InputRulerService irService;
	
	@Autowired
	RulerService rulerService;
	

	@ResponseBody
  	@RequestMapping(value = "/page.inputRulerList.ashx")
	 public Map orderList(Integer page,Integer rows,InputRuler ir) {
		//每页的开始记录
        int start = (page-1)*rows;
        Page p = new Page(start,rows,ir);
		Map map = new HashMap();
		try{
			List<InputRuler> list = irService.list(p);
			Integer count = irService.count(ir);

			log.info(list);
			log.info(count);
			
			map.put("total",count );//total键 存放总记录数，必须的  
			map.put("rows",list);//rows键 存放每页记录 list 
			
		
			
		}catch(Exception e){
			log.error(getTrace(e));
			map.put("meg", "失败！");
			map.put("status", "0");
		}
		
	    return map;  
	  }
	
	@ResponseBody
	@RequestMapping(value = "/page.saveInputRuler.ashx")
	public String saveInputRuler(InputRuler inputRuler) {
		try{	
			inputRuler.setCreateTime(new Date());
			inputRuler.setCreatorID(this.getSessionStaff().getsId().toString());
  			irService.insert(inputRuler);
		}catch(Exception e){
			log.error(getTrace(e));
			return "fail";
		}
		
		
	    return "success";  
	  }
	

	@RequestMapping(value = "/page.inputrulerdetails.ashx")
	 public ModelAndView OfflineOrderDetails(@RequestParam String iRId){
		InputRuler ir = irService.get(iRId);
	    return new ModelAndView("/inputruler/inputrulerdetails.jsp","ir",ir);  
	  }
	
	@ResponseBody
	@RequestMapping(value = "/page.inputrulerdel.ashx")
	 public Map<String,String> OfflineOrderDel(@RequestParam String iRId){
		Map<String,String> resultMap = new HashMap<String, String>();
		resultMap.put("status", "1");
		irService.del(iRId);

	    return resultMap;  
	  }
	
	
	@ResponseBody
	@RequestMapping(value = "/page.processInputRuler.ashx")
	 public Map<String,String> processInputRuler(@RequestBody String b){
		
		log.info(b);
		Map<String,String> resultMap = new HashMap<String, String>();
		Map<String, String> map = new HashMap<String, String>();
		InputRuler ir = new InputRuler();
		ir.setiRDoneStaffID(this.getSessionStaff().getsId().toString());

		resultMap.put("status", "1");
		
		
		String[] split = b.split("&");
		
		for (String string : split) {
			String[] keyValue = string.split("=");
			map.put(keyValue[0], keyValue[1]);
		}
		List<String> opcodes = new ArrayList<String>();
		StringBuffer sb = new StringBuffer();
		
		for (Map.Entry<String, String> entry : map.entrySet()) {  
			if(entry.getKey().contains("opcode")){
				boolean isReduplicate = opcodes.contains(entry.getValue());
				if(isReduplicate){
					resultMap.put("status", "0");
					resultMap.put("id", entry.getKey());
					resultMap.put("sn", entry.getValue());					
					return resultMap;
				}else{
					opcodes.add(entry.getValue());
					sb.append(entry.getValue()+",");
					Ruler ruler = rulerService.find3BySn(entry.getValue());
					if(ruler==null){
						resultMap.put("status", "3");
						resultMap.put("sn", entry.getValue());
						resultMap.put("id", entry.getKey());
						return resultMap;
					}
				}
		    }
		} 
		
		String string = sb.toString();
		String opcodeStr = string.substring(0, string.length()-1);

		Long time = System.currentTimeMillis()/1000;
		ir.setiRDoneTime(time);
		ir.setiRIsDone(1);
		ir.setiRStartStopCode(opcodeStr);
		ir.setiRId(Long.parseLong(map.get("iRId")));
		ir.setiRNumber(Integer.parseInt(map.get("iRNumber")));
		irService.update(ir,opcodes,time);
		return resultMap;  
	  }
	
	
/*	@ResponseBody
	@RequestMapping(value = "/page.processInputRuler.ashx")
	 public Map<String,String> processInputRuler(@RequestBody String b){
		
		log.info(b);
		Map<String,String> resultMap = new HashMap<String, String>();
		Map<String, String> map = new HashMap<String, String>();
		InputRuler ir = new InputRuler();

		resultMap.put("status", "1");
		
		//opnumberinput=2&
		//opcodes=AA000001%2CAA000002&
		//start-stop-code=AA000001%7EAA000002&
		//cvproposer=%E5%B0%9A%E7%91%9E%E5%B2%97&
		//cvkuguan=%E5%B4%94%E7%A4%BC%E5%A0%82
		
		String[] split = b.split("&");
		for (String string : split) {
			String[] keyValue = string.split("=");
			map.put(keyValue[0], URLDecoder.decode(keyValue[1],"utf-8"));
		}
		
		Long time = System.currentTimeMillis()/1000;
		ir.setiRDoneTime(time);
		ir.setiRIsDone(1);
		ir.setiRNumber(Integer.parseInt(map.get("opnumberinput")));
		ir.setiRProposer(map.get("cvproposer"));
		ir.setiRStartStopCode(map.get("start-stop-code"));
		ir.setiRStorekeeper(map.get("irkuguan"));
		ir.setiRRemark(map.get("irremark"));
		
		String opcodes = map.get("opcodes");
		String[] opcodeArr = opcodes.split(",");
		List<String> listCodes = Arrays.asList(opcodeArr);
		irService.insert(ir,listCodes,time);
		
		return resultMap;  
	  }*/
	

	
	
}
